package com.chixing.service.impl;


import com.chixing.mapper.MemberMapper;
import com.chixing.pojo.Member;
import com.chixing.service.MemberService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * dependency injection [DI]
 * service 依赖于 mapper
 *
 * mapper 对象 如何注入到 service中？
 *
 * （1）构造器注入
 * （2）setter 注入
 */

@Service //<bean id="memberServiceImpl" class="com.aistar.service.impl.MemberServiceImpl">
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Member getById(int id) {
       return memberMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean save(Member member) {
        return memberMapper.insert(member)>0;
    }

    @Override
    public boolean update(Member member) {
        return false;
    }

    @Override
    public boolean delete(Integer id) {
        int row = memberMapper.deleteByPrimaryKey(id);
        return row>0;
    }

    @Override
    public boolean getByOthers(String memberName, String memberPwd) {
        return false;
    }

    @Override
    public List<Member> getAll() {
        return memberMapper.selectByExample(null);
    }

    @Override
    public PageInfo<Member> getByPage(Integer pageNum) {
        PageHelper.startPage(pageNum,2);
        List<Member> memberList = memberMapper.selectByExample(null);
        PageInfo<Member> memberPageInfo = new PageInfo<>(memberList);
        return memberPageInfo;
    }
}
